package com.pi.shisan.component;

import com.pi.shisan.component.util.ComponentThread;
import com.pi.shisan.util.spipoint.DataWriter;
import com.pi.shisan.util.spipoint.MultipleXyToSpiConverter;

public abstract class BaseAbstractComponent implements BaseComponent{

    protected MultipleXyToSpiConverter spi;
    private Runnable flusher;

    /**
     * 组件坐标X
     */
    protected int startX;
    /**
     * 组件坐标Y
     */
    protected int startY;
    /**
     * 组件最大宽度
     */
    protected int width;
    /**
     * 组件最大高度
     */
    protected int height;

    public BaseAbstractComponent(MultipleXyToSpiConverter spi, DataWriter dataWriter) {
        this.spi = spi;
        this.flusher = dataWriter::flushData;
    }

    @Override
    public void setX(int x) {
        this.startX = x;
    }

    @Override
    public void setY(int y) {
        this.startY = y;
    }

    @Override
    public void setW(int w) {
        this.width = w;
    }

    @Override
    public void setH(int h) {
        this.height = h;
    }

    @Override
    public void flushData() {
        ComponentThread.execute(flusher);
    }

    @Override
    public void lightLevel(int level) {
        if (spi instanceof DataWriter) {
            ((DataWriter)spi).lightLevel(level);
        }
    }

    @Override
    public void start() {
    }

    @Override
    public void destroy() {
    }
}
